package com.spzx.product.controller;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONArray;
import com.github.pagehelper.PageHelper;
import com.spzx.common.core.exception.ServiceException;
import com.spzx.common.core.web.controller.BaseController;
import com.spzx.common.core.web.domain.AjaxResult;
import com.spzx.common.core.web.page.TableDataInfo;
import com.spzx.product.domain.Product;
import com.spzx.product.domain.ProductSku;
import com.spzx.product.query.SkuQuery;
import com.spzx.product.service.*;
import com.spzx.product.vo.CategoryVo;
import com.spzx.product.vo.IndexDataVo;
import com.spzx.product.vo.ItemVo;
import com.spzx.product.api.domain.ProductSkuVo;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@Tag(name = "聚合数据")
@RestController
@RequestMapping("/channel")
@Slf4j
public class ChannelController extends BaseController {

    @Autowired
    private ICategoryService categoryService;

    @Autowired
    private IProductSkuService productSkuService;

    @Autowired
    private IBrandService brandService;

    @Autowired
    private IProductService productService;

    @Autowired
    private ISkuStockService skuStockService;

    @Autowired
    private IProductDetailsService productDetailsService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Operation(summary = "获取首页数据")
    @GetMapping("/index")
    public AjaxResult index() {
        List<CategoryVo> levelOneCategory = categoryService.getLevelOneCategory();
        List<ProductSkuVo> topSale = productSkuService.getTopSale();
        IndexDataVo indexDataVo = new IndexDataVo();
        indexDataVo.setCategoryList(levelOneCategory);
        indexDataVo.setProductSkuList(topSale);
        return success(indexDataVo);
    }

    @Operation(summary = "获取分类嵌套列表")
    @GetMapping( "/category")
    public AjaxResult category() {
        return success(categoryService.tree());
    }

    @Operation(summary = "显示商品列表")
    @GetMapping("/skuList/{pageNum}/{pageSize}")
    public TableDataInfo skuList(
            @Parameter(description = "当前页码")
            @PathVariable Integer pageNum,

            @Parameter(description = "每页记录数")
            @PathVariable Integer pageSize,

            SkuQuery skuQuery) {

        PageHelper.startPage(pageNum, pageSize);
        List<ProductSkuVo> list = productSkuService.selectProductSkuList(skuQuery);
        return getDataTable(list);
    }

    @Operation(summary = "获取全部品牌")
    @GetMapping("/brand")
    public AjaxResult selectBrandAll() {
        return success(brandService.list());
    }

    @Operation(summary = "商品详情")
    @GetMapping("/item/{skuId}")
    public AjaxResult item(
            @Parameter(description = "skuId")
            @PathVariable Long skuId) {


        //先查询bitmap
        Boolean result = redisTemplate.opsForValue().getBit("product:sku:data:ids", skuId);
        if(!result){
            log.error("商品不存在");
            throw new ServiceException("商品不存在");
        }


        //获取商品sku
        ProductSku productSku = productSkuService.getProductSkuById(skuId);
        //获取商品id
        Long productId = productSku.getProductId();

        ItemVo itemVo = new ItemVo();
        //sku信息
        itemVo.setProductSku(productSku);
        //价格
        itemVo.setSkuPrice(productSkuService.getSkuPrice(skuId));
        //库存
        itemVo.setSkuStock(skuStockService.getSkuStock(skuId));

        //轮播图
        Product product = productService.getById(productId);
        String[] sliderUrls = product.getSliderUrls().split(",");
        itemVo.setSliderUrlList(sliderUrls);

        //规格信息的json对象
        String specValue = product.getSpecValue();
        JSONArray jsonArray = JSON.parseArray(specValue);
        itemVo.setSpecValueList(jsonArray);

        //规格信息的key、value
        itemVo.setSkuSpecValueMap(productSkuService.getSkuSpecValue(productId));

        //商品详情图片
        itemVo.setDetailsImageUrlList(productDetailsService.getProductDetails(productId));

        return success(itemVo);
    }
}